{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3b50350",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from webdriver_manager.chrome import ChromeDriverManager\n",
    "\n",
    "if not os.getenv(\"OPENAI_API_KEY\"):\n",
    "    print(\"FATAL ERROR: OPENAI_API_KEY environment variable is not set.\")\n",
    "else:\n",
    "    print(\"OpenAI API key found.\")\n",
    "    client = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b32c96b",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_prompt = \"\"\"You are a snarky assistant that analyzes the contents of a website,\n",
    "and provides a short, snarky, humorous summary, ignoring text that might be navigation related.\n",
    "Respond in markdown. Do not wrap the markdown in a code block - respond just with the markdown.\n",
    "\"\"\"\n",
    "\n",
    "user_prompt_prefix = \"\"\"\n",
    "Here are the contents of a website.\n",
    "Provide a short summary of this website.\n",
    "If it includes news or announcements, then summarize these too.\n",
    "\n",
    "WEBSITE CONTENT:\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a28f38c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fetch_website_contents(url):\n",
    "    print(f\"Fetching content from {url} using Selenium...\")\n",
    "    driver = None\n",
    "    \n",
    "    try:\n",
    "        print(\"1. Setting up Chrome options...\")\n",
    "        chrome_options = Options()\n",
    "        chrome_options.add_argument(\"--headless\")\n",
    "        chrome_options.add_argument(\"--disable-gpu\")\n",
    "        chrome_options.add_argument(\"window-size=1200x600\")\n",
    "        chrome_options.add_argument(\"--no-sandbox\")\n",
    "        chrome_options.add_argument(\"--disable-dev-shm-usage\")\n",
    "\n",
    "        print(\"2. Installing/finding WebDriver...\")\n",
    "        driver_path = ChromeDriverManager().install()\n",
    "        print(f\"WebDriver is at: {driver_path}\")\n",
    "        service = Service(driver_path)\n",
    "        \n",
    "        print(\"3. Initializing WebDriver...\")\n",
    "        driver = webdriver.Chrome(service=service, options=chrome_options)\n",
    "        \n",
    "        print(\"4. Setting page load timeout...\")\n",
    "        driver.set_page_load_timeout(10)\n",
    "\n",
    "        print(f\"5. Getting URL: {url}...\")\n",
    "        driver.get(url)\n",
    "        \n",
    "        print(\"6. Setting implicit wait...\")\n",
    "        driver.implicitly_wait(5)\n",
    "        \n",
    "        print(\"7. Finding body content...\")\n",
    "        body_content = driver.find_element(By.TAG_NAME, \"body\").text\n",
    "        \n",
    "        print(\"Content fetched successfully.\")\n",
    "        return body_content\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f\"Error during Selenium operation: {e}\")\n",
    "        if driver:\n",
    "            body_content = driver.find_element(By.TAG_NAME, \"body\").text\n",
    "            if body_content:\n",
    "                print(\"Page timed out, but returning partial content.\")\n",
    "                return body_content\n",
    "        return None\n",
    "    finally:\n",
    "        if driver:\n",
    "            print(\"8. Shutting down WebDriver...\")\n",
    "            driver.close()\n",
    "            driver.quit()\n",
    "            print(\"WebDriver shut down.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48cbe11e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def summarize(url):\n",
    "    website_content = fetch_website_contents(url)\n",
    "    \n",
    "    if not website_content:\n",
    "        return \"Sorry, I couldn't fetch the website. It's probably broken or my robot eyes can't see it.\"\n",
    "\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": system_prompt},\n",
    "        {\"role\": \"user\", \"content\": user_prompt_prefix + website_content}\n",
    "    ]\n",
    "\n",
    "    try:\n",
    "        print(\"9. Calling OpenAI API...\")\n",
    "        response = client.chat.completions.create(\n",
    "            model=\"gpt-4-turbo\",\n",
    "            messages=messages\n",
    "        )\n",
    "        print(\"OpenAI call successful.\")\n",
    "        return response.choices[0].message.content\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f\"Error calling OpenAI: {e}\")\n",
    "        return \"Error from AI: It probably got bored and left.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22ebac5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "target_url = \"https://example.com\"\n",
    "snarky_summary = summarize(target_url)\n",
    "\n",
    "print(\"\\n--- SNARKY SUMMARY ---\")\n",
    "print(snarky_summary)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm-engineering",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
